package com.bobby.shop.web;

import com.bobby.shop.pojo.Article;
import com.bobby.shop.pojo.ShopCar;
import com.bobby.shop.pojo.User;
import com.bobby.shop.service.ArticleService;
import com.bobby.shop.service.OrderService;
import com.bobby.shop.service.ShopCarService;
import com.bobby.shop.util.ResponseCode;
import com.bobby.shop.util.ServerResponse;
import com.bobby.shop.vo.OrderDetailVO;
import com.bobby.shop.vo.ShopCarVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Description TODO
 * @Date 2020/12/22 19:39
 * @Created wenjunpei
 */
@Controller
public class ShopCarController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ShopCarService shopCarService;

    @Autowired
    private OrderService orderService;

    @GetMapping("/toOrderDetailPage")
    public ModelAndView forwardToOrderDetailPage(HttpSession session) {
        ModelAndView mv = new ModelAndView();
        User user = (User) session.getAttribute("user");
        List<OrderDetailVO> orderDetailVOList =  orderService.findByUserId(user.getId());
        mv.setViewName("orderDetail");
        mv.addObject("orderDetailVOList",orderDetailVOList);
        return mv;
    }

    @GetMapping("/createOrder")
    @ResponseBody
    public ServerResponse createOrder(HttpSession session) {
        List<ShopCarVO>  selectedShopCarList = (List<ShopCarVO>) session.getAttribute("selectedShopCarList");
        User user = (User) session.getAttribute("user");
        for (ShopCarVO shopCarVO : selectedShopCarList) {
            if(shopCarVO.getBuyNum() > shopCarVO.getStorage()) {
                return ServerResponse.createByError(ResponseCode.ARTICLE_STORAGE_IS_NOT_ENOUGHT);
            }
        }
        orderService.save(selectedShopCarList,user);
        return ServerResponse.createBySuccess();
    }

    @GetMapping("/toOrderPage")
    public ModelAndView forwardToOrderPage(HttpSession session) {
        ModelAndView mv = new ModelAndView();
        List<ShopCarVO>  selectedShopCarList = (List<ShopCarVO>) session.getAttribute("selectedShopCarList");
        User user = (User) session.getAttribute("user");
        mv.addObject("selectedShopCarList",selectedShopCarList);
        mv.addObject("user",user);
        mv.setViewName("order");
        return mv;
    }

    @PostMapping("/submitShopCar")
    @ResponseBody
    public ServerResponse submitShopCar(@Param("shopCarIdStr") String shopCarIdStr,
                                           @Param("shopCarBuyNumStr") String shopCarBuyNumStr,
                                           HttpSession session) {
        if(StringUtils.isEmpty(shopCarIdStr) || StringUtils.isEmpty(shopCarBuyNumStr)) {
            return ServerResponse.createByError(ResponseCode.PARAM_IS_ERROR);
        }
        String[] shopCarIdArray = shopCarIdStr.split(",");
        String[] shopCarBuyNumArray = shopCarBuyNumStr.split(",");
        if(shopCarIdArray.length != shopCarBuyNumArray.length) {
            return ServerResponse.createByError(ResponseCode.PARAM_IS_ERROR);
        }
        User user = (User) session.getAttribute("user");
        int index = 0;
        for (String shopCarId : shopCarIdArray) {
            ShopCar shopCar = shopCarService.findByShopCarId(shopCarId);
            if(shopCar == null) {
                return ServerResponse.createByError(ResponseCode.PARAM_IS_ERROR);
            }else {
                shopCar.setBuynum(Integer.parseInt(shopCarBuyNumArray[index]));
                shopCarService.updateShopCar(shopCar);
            }
            index++;
        }
        List<ShopCarVO> shopCarVOList = shopCarService.findByUserId(user.getId());
        List<ShopCarVO> toShowInOrderPageList = new ArrayList<>();
        for (String shopCarId : shopCarIdArray) {
            Boolean isExist = false;
            for (ShopCarVO shopCarVO : shopCarVOList) {
                if(shopCarVO.getShopCarId().equals(Integer.parseInt(shopCarId))) {
                    isExist = true;
                    toShowInOrderPageList.add(shopCarVO);
                    break;
                }
            }
            if(!isExist) {
                return ServerResponse.createByError(ResponseCode.PARAM_IS_ERROR);
            }
        }

        session.setAttribute("selectedShopCarList",toShowInOrderPageList);
        return ServerResponse.createBySuccess();

    }

    @GetMapping("/deleteByShopCarId")
    @ResponseBody
    public ServerResponse deleteByShopCarId(String shopCarId) {
        if(StringUtils.isEmpty(shopCarId)) {
            return ServerResponse.createByError(ResponseCode.SHOPCAR_ID_IS_NULL);
        }
        ShopCar shopCar = shopCarService.findByShopCarId(shopCarId);
        if(shopCar == null) {
            return ServerResponse.createByError(ResponseCode.SHOPCAR_ID_IS_NULL);
        }
        shopCarService.deleteByShopCarId(shopCarId);
        return ServerResponse.createBySuccess("删除成功");
    }

    @GetMapping("/toShopCarPage")
    public ModelAndView forwardToShopCarPage(HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<ShopCarVO> shopCarVOList = null;
        if(user != null) {
            shopCarVOList = shopCarService.findByUserId(user.getId());
        }
        ModelAndView mv = new ModelAndView();
        mv.setViewName("shopCar");
        mv.addObject("shopCarVOList",shopCarVOList);
        return mv;
    }

    @GetMapping("/addToShopCar")
    @ResponseBody
    public ServerResponse addToCart(String articleId, String articleNum, HttpSession session) {
        if(StringUtils.isEmpty(articleId)) {
            return ServerResponse.createByError(ResponseCode.ARTICLE_ID_IS_NULL);
        }
        Article article = articleService.findByArticleId(articleId);
        if(article == null) {
            return ServerResponse.createByError(ResponseCode.ARTICLE_ID_IS_ERROR);
        }
        if(StringUtils.isEmpty(articleNum)) {
            return ServerResponse.createByError(ResponseCode.ARTICLE_NUM_IS_NULL);
        }
        Integer num = Integer.parseInt(articleNum);
        if(num < 1 || num > Integer.MAX_VALUE) {
            return ServerResponse.createByError(ResponseCode.ARTICLE_NUM_IS_ERROR);
        }
        if(article.getStorage() < Integer.parseInt(articleNum)) {
            return ServerResponse.createByError(ResponseCode.ARTICLE_STORAGE_IS_NOT_ENOUGHT);
        }
        User user = (User) session.getAttribute("user");
        ShopCar shopCar = shopCarService.findByArticleIdAndUserId(articleId,user.getId());
        if(shopCar == null) {
            shopCar = new ShopCar();
            shopCar.setArticleid(Integer.parseInt(articleId));
            shopCar.setBuynum(Integer.parseInt(articleNum));
            shopCar.setUserid(user.getId());
        }else {
            shopCar.setBuynum(shopCar.getBuynum() + Integer.parseInt(articleNum));
        }
        shopCarService.save(shopCar);
        return ServerResponse.createBySuccess("加入购物车成功");
    }
}
